package com.note.simpleadmin.module.system.controller;
import com.note.simpleadmin.cache.EnCacheUtil;
import com.note.simpleadmin.utils.FileUtil;
import com.note.simpleadmin.utils.MD5Utils;
import com.note.simpleadmin.utils.MimeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("system/plugs")
public class PlugsController
{
//    $uptype = $this->request->get('uptype');
//    if (!in_array($uptype, ['local', 'qiniu', 'oss'])) {
//        $uptype = sysconf('storage_type');
//    }
//    $mode = $this->request->get('mode', 'one');
//    $types = $this->request->get('type', 'jpg,png');
//    $this->assign('mimes', FileService::getFileMine($types));
//    $this->assign('field', $this->request->get('field', 'file'));
//    return view('', ['mode' => $mode, 'types' => $types, 'uptype' => $uptype]);
	@RequestMapping("upfile")
	public String upfile(HttpServletRequest request)
	{
		String uptype=EnCacheUtil.getKey("system_storage_type");
		String mode=request.getParameter("mode");
		String field=request.getParameter("field");
		String types=request.getParameter("type");
		request.setAttribute("mime", MimeUtil.getString(types.split(",")));
		request.setAttribute("uptype",uptype);
		request.setAttribute("mode",mode);
		request.setAttribute("field",field);
		request.setAttribute("types",types);
		return "plugins/upfile";
	}
	@RequestMapping("upload")
	@ResponseBody
	public Map<String,Object> upload(HttpServletRequest request)
	{
		Map<String,Object> result=new HashMap<String,Object>();
		String dir= EnCacheUtil.getKey("system_storage_path");
		String base_url=EnCacheUtil.getKey("system_base_url");
		List<Map<String,String>> list= FileUtil.fileupload(request,dir,base_url);
		if(list.size()>0)
		{
			Map<String,String> fileInfo=list.get(0);
			Map<String,Object> data=new HashMap<String,Object>();
			data.put("site_url",fileInfo.get("url"));
			result.put("code","SUCCESS");
			result.put("msg","文件上传成功");
			result.put("data",data);
		}
		else
		{
			result.put("code","ERROR");
			result.put("msg","文件上传失败");
		}
		return result;
	}

	@RequestMapping("upstate")
	@ResponseBody
	public Map<String,Object> upstate(HttpServletRequest request, String md5, String filename, String uptype, String id)
	{
		Map<String,Object> result=new HashMap<String,Object>();
		filename=md5+filename.substring(filename.lastIndexOf("."));
		String code="NOT_FOUND";
		result.put("code",code);
		result.put("msg","");
		result.put("time",System.currentTimeMillis()+"");
		Map<String,String>  data=new HashMap<String,String>();
		data.put("file_url",filename);
		data.put("server",request.getContextPath()+"/system/plugs/upload");
		data.put("uptype","local");
		data.put("token", MD5Utils.encode("123"));
		result.put("data",data);
		return result;
	}















}
